<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { BaseinfoDemandApi } from '#/api/csyt/baseinfodemand';

import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';

import { Page, useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  deleteBaseinfoDemand,
  getBaseinfoDemandPage,
} from '#/api/csyt/baseinfodemand';
import { $t } from '#/locales';

import { useGridColumns, useGridFormSchema } from './data';
import DemandStatusForm from './modules/baseinfo-demand-status.vue';
import Form from './modules/form.vue';

const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});

const [DemandStatusModal] = useVbenModal({
  connectedComponent: DemandStatusForm,
  destroyOnClose: true,
});

const router = useRouter();
const route = useRoute();
console.log(route);
/** 刷新表格 */
function onRefresh() {
  gridApi.query();
}

/** 编辑需求管理 */
function handleEdit(row: BaseinfoDemandApi.BaseinfoDemand) {
  formModalApi.setData(row).open();
}

/** 编辑需求管理 */
function handleEditStatus(row: BaseinfoDemandApi.BaseinfoDemand) {
  if (router) {
    router.push({
      path: '/gxgl/prod2deman',
      query: {
        id: row.id,
        demandName: row.demandName,
        demandNo: row.demandNo,
        phone: row.phone,
        companyName: row.companyName,
      },
    });
  }
}

/** 删除需求管理 */
async function handleDelete(row: BaseinfoDemandApi.BaseinfoDemand) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.id]),
    key: 'action_key_msg',
  });
  try {
    await deleteBaseinfoDemand(row.id as number);
    message.success({
      content: $t('ui.actionMessage.deleteSuccess', [row.id]),
      key: 'action_key_msg',
    });
    onRefresh();
  } finally {
    hideLoading();
  }
}

const checkedIds = ref<number[]>([]);
function handleRowCheckboxChange({
  records,
}: {
  records: BaseinfoDemandApi.BaseinfoDemand[];
}) {
  checkedIds.value = records.map((item) => item.id);
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    pagerConfig: {
      enabled: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getBaseinfoDemandPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<BaseinfoDemandApi.BaseinfoDemand>,
  gridEvents: {
    checkboxAll: handleRowCheckboxChange,
    checkboxChange: handleRowCheckboxChange,
  },
});
</script>

<template>
  <Page auto-content-height>
    <FormModal @success="onRefresh" />
    <DemandStatusModal @success="onRefresh" />
    <Grid table-title="需求管理列表">
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: '详情',
              type: 'link',
              icon: ACTION_ICON.VIEW,
              onClick: handleEdit.bind(null, row),
            },
            {
              label: '感兴趣',
              type: 'link',
              icon: ACTION_ICON.ADD,
              ifShow: true,
              onClick: handleEditStatus.bind(null, row),
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>
